// pages/weather.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    province: '',
    city: '',
    district: '',
    showImage: false,
    showLoading: false,
    nowWeather: {},
    lifestyle: {},
    txtId:"",
    showTxt:true
  },

  /*
自定义方法
 */
  getChangeRegion: function(e) {
    e.detail.value;
    this.setData({
      province: e.detail.value[0],
      city: e.detail.value[1],
      district: e.detail.value[2]
    });
    this.getNowWeaterDetail();
    this.getLifestyle();
  },
  // 获取当前天气信息
  getNowWeaterDetail: function(e) {
    var that = this;
    wx.request({
      url: 'https://free-api.heweather.net/s6/weather/now?',
      data: {
        location: that.data.city,
        key: '214938f171674078b8c2d80631122f25'
      },
      success: function(res) {
        that.setData({
          nowWeather: res.data.HeWeather6[0].now
        });
        that.setData({
          showImage: true
        });
      },
      fail: function(error) {
        console.log(error);
      }
    });
  },

  // 获取生活指数
  getLifestyle: function(e) {
    var that = this;
    wx.request({
      url: 'https://free-api.heweather.net/s6/weather/lifestyle?',
      data: {
        location: that.data.city,
        key: '214938f171674078b8c2d80631122f25'
      },
      success: function(res) {
        that.setData({
          lifestyle: res.data.HeWeather6[0].lifestyle
        });
      }
    });
  },
  //  获取当前位置信息
  getLocation: function(e) {
    var that = this;
    var QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');
    var qqmapsdk;
    qqmapsdk = new QQMapWX({
      key: 'QZUBZ-ZUFLO-ZIQW5-S3H6Y-6SBT3-UXFL6'
    });
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: res.latitude,
            longitude: res.longitude
          },
          success: function(addressRes) {
            that.setData({
              province: addressRes.result.address_component.province,
              city: addressRes.result.address_component.city,
              district: addressRes.result.address_component.district
            });

            that.getNowWeaterDetail();
            that.getLifestyle();
            that.setData({
              showLoading: true
            });
          },
          fail: function(error) {
            console.log(error);
          }
        });
      }
    });
  },
  // 改变生活指数详情
  changeTxt:function(styId){
    var that = this
    
    if (styId.currentTarget.dataset.id === that.data.txtId){
      that.setData(
        {
          showTxt: !that.data.showTxt
        }
      )
    }else{
      that.setData(
        {
          txtId: styId.currentTarget.dataset.id,
          showTxt: false
        }
      )
    }
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    this.getLocation();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {}
});